import { TextList, TextList2 } from "../Config";
import ItemView from "./ItemView";
import GridLayerListView from "../engine/GridLayerListView";
import CCPoolManager from "../engine/pool/CCPoolManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GridLayerListViewTest extends cc.Component {


    @property(GridLayerListView)
    gridListView: GridLayerListView = null;

    @property(cc.Prefab)
    itemPrefab: cc.Prefab = null;

    private useIndex: number = 0;//道具重置使用

    private isDown: boolean = false;


    start() {
        if (this.itemPrefab) {
            CCPoolManager.instance().init("ItemView", this.itemPrefab, 20)
        }

        this.gridListView.totalCount = TextList.length;

    }

    updateItem(listView, pos: number, item: cc.Node) {
        // console.log(' updateItem pos ', pos, item.active)
        let text = ''
        if (this.useIndex == 1) {
            text = TextList2[pos]
        } else {
            text = TextList[pos]
        }
        let itemView: ItemView = item.getComponent(ItemView);
        itemView.content = text;
        itemView.refresh()
    }


    onButtonClick() {
        console.log(' onButtonClick useIndex ', this.useIndex)
        this.isDown = false;
        if (this.useIndex == 0) {
            this.useIndex = 1;
            this.gridListView.reset(TextList2.length)
        } else {
            this.useIndex = 0;
            this.gridListView.reset(TextList.length)
        }

    }
    onButtonDownClick() {
        console.log(' onButtonDownClick useIndex ', this.useIndex)
        if (!this.isDown) {
            this.isDown = true;
            // this.gridListView.scrollToBottom()
            this.gridListView.scrollToOffset(8)
        }

    }

    onButtonUpClick() {
        console.log(' onButtonUpClick useIndex ', this.useIndex)
        if (this.isDown) {
            this.isDown = false;
            // this.gridListView.reset(TextList.length)
            this.gridListView.scrollToTop()
        }

    }
    // update (dt) {}

    onBackClick() {
        this.node.destroy()
    }

    // update (dt) {}
}
